﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TodayWhoModel;
using System.Web.Services;

namespace TodayWho
{
    public partial class Master : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            form1.Action = Request.RawUrl;
            txtSearch.Attributes.Add("onfocus", "clearText()");
            txtSearch.Attributes.Add("onblur", "resetText()");
            if (Session["userId"] == null && IsValidAuthCookie(Request.Cookies["todaywho"], "salt"))
            {
                UserModel user = new UserModel();
                user.UserName = Request.Cookies["todaywho"].Value.Split('|')[0];
                user.RetrieveUserIdWithUserName();
                Session.Add("userId", user.UserId);
                Session.Add("userName", user.UserName);
            }
            if (Session["userId"] != null && Session["userName"] != null)
            {
                divLoggedInNav.Attributes["style"] = "display:block";
                divNotLoggedInNav.Attributes["style"] = "display:none";
                aProfile.HRef = "profile.aspx?u=" + Session["userName"].ToString();
            }
            
        }

        protected void btnLogout_OnClick(object sender, EventArgs e)
        {   
            Session.Clear();
            if (Request.Cookies["todaywho"] != null)
            {
                HttpCookie cookie = new HttpCookie(Request.Cookies["todaywho"].Name);
                cookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(cookie);
            }
            Response.Redirect("/home.aspx");
        }

        protected void btnRandom_OnClick(object sender, EventArgs e)
        {
            string username = UserModel.GetRandomUserName();
            if (!string.IsNullOrEmpty(username))
            {
                Response.Redirect("/profile.aspx?u=" + username);
            }
            else
            {
                Response.Redirect("/");
            }
        }

        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("/search.aspx?s=" + txtSearch.Text);
        }

        // Determine whether the given authentication cookie is valid by
        // extracting the username, retrieving the saved password, recomputing its
        // hash, and comparing the hashes to see if they match. If they match,
        // then this authentication cookie is valid.
        public bool IsValidAuthCookie(HttpCookie cookie, string salt)
        {
            if (cookie == null) return false;
            // Split the cookie value by the pipe delimiter.
            string[] values = cookie.Value.Split('|');
            if (values.Length != 2) return false;

            // Retrieve the username and hash from the split values.
            string username = values[0];
            string hash = values[1];

            // You'll have to provide your GetPasswordForUser function.
            string password = UserModel.RetrievePasswordWithUserName(username);

            // Check the password and salt against the hash.
            return Helper.IsMatchingHash(password, salt, hash);
        }

        [WebMethod]
        public static void GetRandom()
        { 
            
        }
    }
}